home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / computePolysetVolume.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  4.5 KB  |  142 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //
  22. //  Creation Date:  2001 
  23. //  Author:         Duncan Brinsmead( from a method suggested by Jos Stam )
  24. //
  25. //<doc>
  26. //<name computePolysetVolume>
  27. //<owner "Alias|Wavefront Unsupported">
  28. //
  29. //<synopsis>
  30. //        computePolysetVolume
  31. //
  32. //<returns>
  33. //        None.    
  34. //
  35. //<description>
  36. //        Prints the total volume of all polysets on the pick list.
  37. //        For accurate results the geometry should be closed, with no
  38. //        holes or minimal gaps and no interpenetrating surfaces( such as
  39. //        as two overlapping spheres ).
  40. //        The method uses the divergence theorem:
  41. //            \int_{vol} Div(f) dV = int_{surf} Dot(f,n) dS
  42. //        To use it to compute volumes set f=(0,0,z), you then have
  43. //            Volume = \int_{vol} 1 dV = int_{surf} n_z(u,v) du dv
  44. //        Where n_z is the "z" component of the normal to the surface at the parameter value (u,v).
  45. //        If you only have triangles then the formula reads:
  46. //            Volume = sum_{over all triangles} (z0+z1+z2)/3*n_z*A
  47. //
  48. //
  49. //<flags>
  50. //    None.
  51. //
  52. //<examples>
  53. //    // Create a poly cube and find its volume 
  54. //  polyCube;
  55. // // Result: pCube2 polyCube1 //
  56. // computePolysetVolume;
  57. // // pCube3 faces = 6 //
  58. // // TOTAL VOLUME = 1 //
  59. //
  60. //</doc>
  61.  
  62. global proc float triangleArea( float $v1[], float $v2[], float $v3[] )
  63. {
  64.     float $c1[] = crossProduct( $v1, $v2, 0, 0 );
  65.     float $c2[] = crossProduct( $v2, $v3, 0, 0 );
  66.     float $c3[] = crossProduct( $v3, $v1, 0, 0 );
  67.     float $vec[3];
  68.     $vec[0] = $c1[0] + $c2[0] + $c3[0];
  69.     $vec[1] = $c1[1] + $c2[1] + $c3[1];
  70.     $vec[2] = $c1[2] + $c2[2] + $c3[2];
  71.     float $area = sqrt( $vec[0]*$vec[0] + $vec[1]*$vec[1] + $vec[2] * $vec[2])/2.0;
  72.     return( $area );
  73. }
  74.  
  75. global proc float quadArea( float $v1[], float $v2[], float $v3[], float $v4[] )
  76. {
  77.     float $a1 = triangleArea( $v1,$v2,$v3 );
  78.     // float $a2 = triangleArea( $v3,$v4,$v1 );
  79.     float $a2 = triangleArea( $v3,$v4,$v2 );
  80.     return( $a1 + $a2 );
  81. }
  82.  
  83. global proc float computePolysetVolume()
  84. {
  85.     string $pobjList[], $pobjListDup[], $pobj;
  86.  
  87.     $pobjList = `ls -dag -sl -type mesh`;
  88.     if(size( $pobjList ) < 1 ){
  89.         error( "no polygon objects selected for computePolysetVolume." );
  90.         return 0.0;
  91.     }
  92.     $pobjListDup = `duplicate -rr $pobjList`;
  93.  
  94.     select -r $pobjListDup;
  95.     FreezeTransformations;
  96.     
  97.     int $obj, $i, $k;
  98.     string $normalInfo[];
  99.     string $curFace;
  100.     string $ni[];
  101.     string $verts[];
  102.     float $nz;
  103.     float $v1[3], $v2[3], $v3[3], $v4[3];
  104.     float $A;
  105.     int $numFaces[], $nVerts;
  106.     float $totalVolume = 0;
  107.     for( $obj = 0; $obj < size($pobjListDup); $obj++ ) {
  108.         $pobj = $pobjListDup[$obj];
  109.         $numFaces = `polyEvaluate -f $pobj`;
  110.         print( "// " + $pobj + " faces = " +$numFaces[0]+ " //\n" );
  111.         for( $i = 0; $i < $numFaces[0]; $i++ ){
  112.             $curFace = ($pobj + ".f[" + $i + "]");    
  113.             $normalInfo = `polyInfo -faceNormals $curFace`;
  114.             tokenize $normalInfo[0] $ni; 
  115.             $nz = $ni[4];
  116.             $verts = `listAttr $curFace`;
  117.             $nVerts = size( $verts )/4;
  118.             float $val;
  119.             if( $nVerts == 3 ){
  120.                 $v1 = pointPosition( $pobj + "." + $verts[0] );
  121.                 $v2 = pointPosition( $pobj + "." + $verts[4] );
  122.                 $v3 = pointPosition( $pobj + "." + $verts[8] );
  123.                 $A = triangleArea($v1,$v2,$v3);
  124.                 $val = $A * $nz * ( $v1[2] + $v2[2] + $v3[2] )/3.0;
  125.             } else if( $nVerts == 4 ){
  126.                 $v1 = pointPosition( $pobj + "." + $verts[0] );
  127.                 $v2 = pointPosition( $pobj + "." + $verts[4] );
  128.                 $v3 = pointPosition( $pobj + "." + $verts[8] );
  129.                 $v4 = pointPosition( $pobj + "." + $verts[12] );
  130.                 $A = quadArea($v1,$v2,$v3, $v4);
  131.  
  132.                 $val = $A * $nz * ( $v1[2] + $v2[2] + $v3[2] + $v4[2])/4.0;
  133.             }
  134.             $totalVolume += $val;
  135.         }
  136.     }
  137.     print ("// TOTAL VOLUME = " +$totalVolume+ " //\n");
  138.     delete $pobjListDup;
  139.     select -r $pobjList;
  140.     return $totalVolume;
  141. }
  142.